/**
 * Preload 脚本 - 安全的进程间通信
 */

const { contextBridge, ipcRenderer } = require('electron');

contextBridge.exposeInMainWorld('electronAPI', {
  // 启动服务
  startService: (serviceName, serviceType) => 
    ipcRenderer.invoke('start-service', serviceName, serviceType),

  // 停止服务
  stopService: (serviceName) => 
    ipcRenderer.invoke('stop-service', serviceName),

  // 获取服务状态
  getServicesStatus: () => 
    ipcRenderer.invoke('get-services-status'),

  // 打开浏览器
  openUrl: (url) => 
    ipcRenderer.invoke('open-url', url),

  // 获取配置
  getConfig: () => 
    ipcRenderer.invoke('get-config'),

  // 监听服务输出
  onServiceOutput: (callback) => 
    ipcRenderer.on('service-output', (event, data) => callback(data)),

  // 监听服务停止
  onServiceStopped: (callback) => 
    ipcRenderer.on('service-stopped', (event, data) => callback(data)),

  // 移除监听
  removeServiceOutputListener: () => 
    ipcRenderer.removeAllListeners('service-output'),

  removeServiceStoppedListener: () => 
    ipcRenderer.removeAllListeners('service-stopped')
});
